{include file="public/base" /}
{block name="main"}
<link rel="stylesheet" href="__PUBLIC__/admin/layui/css/layui.css"  media="all">
<style>
    .file {
        position: relative;
        display: inline-block;
        background: #D0EEFF;
        border: 1px solid #99D3F5;
        border-radius: 4px;
        padding: 4px 12px;
        overflow: hidden;
        color: #1E88C7;
        text-decoration: none;
        text-indent: 0;
        line-height: 20px;
    }

    .file input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
    }

    .file:hover {
        background: #AADFFD;
        border-color: #78C3F3;
        color: #004974;
        text-decoration: none;
    }
    </style>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑商品</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form method="get" action="javascript:;" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">商品名</label>
                            <div class="col-sm-10">
                                <input type="text" name="goods" class="form-control" value="{$goodsInfo['goods']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">规格</label>
                            <div class="col-sm-10">
                                <input type="text" name="spec" class="form-control" value="{$goodsInfo['spec']}" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否为限购</label>
                            <div class="col-sm-10">
                                <input name="purchase" id="purchase" type="checkbox"  {if $goodsInfo['purchase']==1}checked{/if}>
                            </div>
                            <div class="col-sm-10">
                                <input class="form-control"  name="number" id="number" type="text" placeholder="请输入限购数量" {if $goodsInfo['number']==''}style="display:none"{/if} value="{$goodsInfo['number']}">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">缩略图</label>
                            <div class="col-sm-10">
                                <div>
                                    <div class="layui-upload">
                                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                                        <div class="layui-upload-list">
                                            <img class="layui-upload-img" id="demo1" src="{$goodsInfo['small_img']}">
                                            <p id="demoText"><input type="hidden" name="small_img" value="{$goodsInfo['small_img']}"></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">商品图</label>

                            <div class="col-sm-10">
                                <div>
                                    <div class="layui-upload">
                                        <button type="button" class="layui-btn" id="test2">上传图片</button>
                                        <div class="layui-upload-list">
                                            <img class="layui-upload-img" id="demo3" src="{$goodsInfo['img']}">
                                            <p id="demoTexts"><input type="hidden" name="img" value="{$goodsInfo['img']}"></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">商品简介</label>

                            <div class="col-sm-10">
                                <div class="layui-upload-drag" id="test10">
                                    <i class="layui-icon"></i>
                                    <p>点击上传，或将文件拖拽到此处</p>
                                </div>
                                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                                    预览图：
                                    <div class="layui-upload-list" id="demo2">
                                        {foreach name="$goodsInfo['synopsis']" id="val"}
                                        <img src="{$val}" alt="{$val}" width="220" onclick="del(this,'{$val}')" onmouseover="tips(this)">
                                        {/foreach}
                                    </div>
                                </blockquote>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">推荐星级</label>
                            <div class="col-sm-10">
                                <select name="recommend" class="form-control" required>
                                    <option value="0">0</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                </select>
                                <!--<input type="number" oninput="if(value.length>1)value=value.slice(0,1);" class="form-control" name="recommend" value="{$goodsInfo['recommend']}" required>-->
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">库存</label>

                            <div class="col-sm-10">
                                <input type="number" oninput="if(value<0)value=value.replace('-','')" class="form-control"name="stock" value="{$goodsInfo['stock']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">已售数量</label>
                            <div class="col-sm-10">
                                <input type="number" oninput="if(value<0)value=value.replace('-','')" class="form-control" name="sold" value="{$goodsInfo['sold']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">原价</label>
                            <div class="col-sm-10">
                                <input type="number" oninput="if(value<0)value=value.replace('-','')" class="form-control" name="old_price" value="{$goodsInfo['old_price']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">售价</label>
                            <div class="col-sm-10">
                                <input type="number" oninput="if(value<0)value=value.replace('-','')" class="form-control" name="price" value="{$goodsInfo['price']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">佣金比例</label>
                            <div class="col-sm-10">
                                <input type="number" oninput="if(value<0)value=value.replace('-','')" class="form-control" name="commission" value="{$goodsInfo['commission']}" required>
                            </div>
                        </div>
                        <!--<div class="form-group">-->
                            <!--<label class="col-sm-2 control-label">社区</label>-->
                            <!--<div class="col-sm-10">-->
                                <!--<select name="eid" class="form-control" required>-->
                                    <!--<option value="0">请选择</option>-->
                                    <!--{foreach name="estate" id="value"}-->
                                    <!--<option value="{$value['id']}">{$value['estate']}</option>-->
                                    <!--{/foreach}-->
                                <!--</select>-->
                            <!--</div>-->
                        <!--</div>-->
                        <!--<div class="hr-line-dashed"></div>-->
                        <div id="zhi"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <input type="hidden" name="id" value="{$goodsInfo['gid']|default=0}">
                                <button class="btn btn-primary" type="submit" onclick="edit()">保存内容</button>
                                <!--<button class="btn btn-white" type="submit">取消</button>-->
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

</div>
{/block}
{block name="javascript"}
<script src="ADMIN_JS/content.min.js?v=1.0.0"></script>
<script src="ADMIN_JS/plugins/iCheck/icheck.min.js"></script>
<script src="__PUBLIC__/admin/layui/layui.all.js" charset="utf-8"></script>
<script>
    window.onload=function(){
        $("[name='recommend'] [value='{$goodsInfo.recommend}']").attr('selected',true);
    }
    // var ue = UE.getEditor('editor');
    var clickSwitch = function() {
        if ($("#purchase").is(':checked')) {
            $("#number").attr("style", "display:block");
        } else {
            $("#number").attr("style", "display:none");
        }
    };
    $("#purchase").on('click', function () {
        clickSwitch()
    });
    function edit()
    {
        //实例化编辑器
        //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例

        var goods = $('[name="goods"]').val();
        var spec = $('[name="spec"]').val();
        var small_img = $('[name="small_img"]').val();
        var img = $('[name="img"]').val();
        var recommend = $('[name="recommend"]').val();
        var stock = $('[name="stock"]').val();
        var sold = $('[name="sold"]').val();
        var old_price = $('[name="old_price"]').val();
        var price = $('[name="price"]').val();
        var commission = $('[name="commission"]').val();
        var goodsInfoImg = new Array();
        $('[name="imgs[]"]').each(function(i){
            goodsInfoImg.push($('[name="imgs[]"]').eq(i).val());
        });
        if(goods==''){
            layer.msg('请填写商品名',{icon:6,time:1500});return false;
        }
        if(spec==''){
            layer.msg('请填写商品规格',{icon:6,time:1500});return false;
        }
        if(small_img==''){
            layer.msg('请填上传缩略图',{icon:6,time:1500});return false;
        }
        if(img==''){
            layer.msg('请填上传商品图',{icon:6,time:1500});return false;
        }
        if(goodsInfoImg=='') {
            var checks = '{$goodsInfo["checkSynopsis"]}';
            var arr = checks.split(',');
            if(arr.length<0){
                layer.msg('请上传简介图片',{icon:6,time:1500});return false;
            }else{
                goodsInfoImg = checks;
            }

        }
        if(stock==''){
            layer.msg('请输入库存',{icon:6,time:1500});return false;
        }
        if(sold==''){
            layer.msg('请输入已售数量',{icon:6,time:1500});return false;
        }
        if(old_price==''){
            layer.msg('请输入原价',{icon:6,time:1500});return false;
        }
        if(price==''){
            layer.msg('请输入售价',{icon:6,time:1500});return false;
        }
        if(commission==''){
            layer.msg('请输入佣金比例',{icon:6,time:1500});return false;
        }
        if ($("#purchase").is(':checked')) {
            var number = $('#number').val();
        }else{
            var number = "";
        }
        var id = $("[name='id']").val();
        if(id!=0){
            var url = '{:url("goods/goodsEdit")}';
        }else{
            var url = '{:url("goods/goodsAdd")}';
        }
        var loading = layer.msg('正在提交数据，请稍后...', {icon: 16,time:6000});
        $.ajax({
            type:'post',
            url:url,
            data:{'id':id,'goods':goods,'spec':spec,'small_img':small_img,'img':img,'recommend':recommend,'stock':stock,'sold':sold,'old_price':old_price,'price':price,'synopsis':goodsInfoImg,'number':number,'commission':commission},
            success:function(data){
                layer.close(loading);
                if(data.code==1){
                    layer.msg(data.msg,{icon:6,time:1500});
                     setTimeout(function(){window.parent.location.reload();},1000);
                }else{
                    layer.msg(data.msg,{icon:5,time:1500});
                }
            }
        })
    }

</script>
<script>
    layui.use('upload', function() {
        var $ = layui.jquery
            , upload = layui.upload;
        //缩略图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '{:url("Goods/uploadImg")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    layer.msg('上传成功');
                    //$("#demoText").append('<input type="hidden" name="small_img"  value=' + res.filename + ' >');
                    $("[name='small_img']").val(res.filename);
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        //商品图片上传
        var uploadInst = upload.render({
            elem: '#test2'
            ,url: '{:url("Goods/uploadImg")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo3').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传成功
                if(res.code > 0){
                    layer.msg('上传成功');
                    //$("#demoTexts").append('<input type="hidden" name="img"  value=' + res.filename + ' >');
                    $("[name='img']").val(res.filename);
                }
                //上传失败
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoTexts');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        //拖拽上传
        upload.render({
            elem: '#test10'
            , url: '{:url("Goods/uploadImg")}'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    console.log(index);
                    $('#demo2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="220">')
                });
            }
            , done: function (res) {
                //上传完毕
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 6, time: 1500});
                    $("#zhi").append('<input type="hidden" name="imgs[]" value=' + res.filename + ' >');
                } else {
                    layer.msg(res.msg, {icon: 5, time: 1500});
                }
            }
        });
    });
    function del(index,src){
        var id = {$goodsInfo.id};
        $.ajax({
            type:'post',
            url:'{:url("Goods/delImg")}',
            data:{'src':src,'id':id},
            success:function(res){
                if(res.code==1){
                    layer.msg(res.msg,{icon:6,time:1500});
                    index.remove();
                }else{
                    layer.msg(res.msg,{icon:6,time:1500});
                }
            }
        })
    }
    function tips(indexs){
        layer.tips('单击删除', indexs);
    }

</script>
{/block}